/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ESchedulerManagementPanel.java
 *
 * Created on Feb 6, 2012, 5:12:58 PM
 */
package ui.panels;

import entities.Department;
import entities.EmployeeDepartmentHistory;
import entities.ViewScheduler;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.DepartmentHeadDriver;
import model.ViewESchedulerTableModel;
import ui.windows.RemedySchedulerFrame;

/**
 *
 * @author viev
 */
public class ESchedulerManagementPanel extends javax.swing.JPanel {

    /** Creates new form ESchedulerManagementPanel */
    public ESchedulerManagementPanel(RemedySchedulerFrame mainFrame) {
        this.mainFrame = mainFrame;
        driver = (DepartmentHeadDriver) mainFrame.getDriver();
        initComponents();
        includeModel();
    }
    
    private void includeModel() {
        schedulerTableModel = new ViewESchedulerTableModel(driver.showviewEScheduler(mainFrame.getUser().getDepartmentID()));
        schedulerTable.setModel(schedulerTableModel);
        schedulerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        schedulerTable.setFillsViewportHeight(true);
        schedulerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        schedulerTable.getTableHeader().setFont(new Font(null, Font.BOLD, 10));
        DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
        cellRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer.setBackground(Color.yellow);
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer1.setBackground(new Color(100, 50, 102));
        cellRenderer1.setForeground(new Color(240, 240, 240));
        schedulerTable.getColumnModel().getColumn(0).setPreferredWidth(70);
        schedulerTable.getColumnModel().getColumn(1).setPreferredWidth(200);
        schedulerTable.getColumnModel().getColumn(2).setPreferredWidth(80);
        schedulerTable.getColumnModel().getColumn(2).setCellRenderer(cellRenderer);
        schedulerTable.getColumnModel().getColumn(3).setPreferredWidth(80);
        schedulerTable.getColumnModel().getColumn(3).setCellRenderer(cellRenderer1);
        schedulerTable.getColumnModel().getColumn(4).setPreferredWidth(120);
        schedulerTable.getColumnModel().getColumn(4).setCellRenderer(cellRenderer);
        schedulerTable.getColumnModel().getColumn(5).setPreferredWidth(100);
        schedulerTable.getColumnModel().getColumn(5).setCellRenderer(cellRenderer);
        schedulerTable.getColumnModel().getColumn(6).setPreferredWidth(100);
        
        createSchedulerPanel = new CreateESchedulerPanel(mainFrame, this);
        editSchedulerPanel = new EditESchedulerPanel(mainFrame, this);
        
        filter();
        target = 1;
    }
    
    private void swithAction() {
        int choise = cbActions.getSelectedIndex();
        switchTarget(choise);
        cbActions.setSelectedIndex(0);
    }

    private void switchTarget(int choise) {
        if (target != choise || target == 4) {
            switch (choise) {
                case 0:
                    break;
                case 1:
                    this.remove(viewControl);
                    txtID.setEnabled(true);
                    txtName.setEnabled(true);
                    viewControl = new JScrollPane(schedulerTable);
                    target = 1;
                    this.add(viewControl, BorderLayout.CENTER);
                    this.revalidate();
                    break;
                case 2:
                    this.remove(viewControl);
                    txtID.setEnabled(false);
                    txtName.setEnabled(false);
                    viewControl = new JScrollPane(createSchedulerPanel);
                    target = 2;
                    this.add(viewControl, BorderLayout.CENTER);
                    this.revalidate();
                    break;
                case 3:
                    setSelectedEmployee();
                    if (selectedRecord != null) {
                        txtID.setEnabled(false);
                        txtName.setEnabled(false);
                        this.remove(viewControl);
                        viewControl = new JScrollPane(editSchedulerPanel);
                        this.add(viewControl, BorderLayout.CENTER);
                        this.revalidate();
                        target = 3;
                    } else {
                        JOptionPane.showMessageDialog(null, "You migh select one record !");
                    }
                    break;
                case 4:
                    new Thread() {

                        @Override
                        public void run() {
                            deleteScheduler();
                        }
                    }.start();
                    break;
            }
        }
    }

    public void showAllScheduler() {
        switchTarget(1);
    }

    public void showCreateScheduler() {
        switchTarget(2);
    }

    public boolean createEmployeeDepartmentH(EmployeeDepartmentHistory record) {
        boolean returnVal = driver.insertEmployeeDepH(record);
        if (returnVal) {
            ViewScheduler view = driver.showViewSchedulerByPK(record.getEmployeeID(), record.getDepartmentID(),
                    record.getShiftID(), record.getStartDate());
            schedulerTableModel.addViewScheduler(view);
        }
        return returnVal;
    }

    public boolean updateScheduler(ViewScheduler view) {
        EmployeeDepartmentHistory record = new EmployeeDepartmentHistory();
        record.setEmployeeID(view.getEmployeeID());
        record.setDepartmentID(view.getDepartmentID());
        record.setShiftID(view.getShiftID());
        record.setStartDate(view.getStartDate());
        record.setEndDate(view.getEndDate());
        boolean returnVal = driver.updateEmployeeDepH(record);
        if (returnVal) {
            record.setModifiedDate(Calendar.getInstance().getTime());
            schedulerTableModel.updateViewScheduler(view);
        }
        return returnVal;
    }

    private void deleteScheduler() {
        int index = schedulerTable.getSelectedRow();
        if (index != -1) {
            target = 4;
            int choise = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this record !", "Delete", JOptionPane.YES_NO_OPTION);
            if (choise == JOptionPane.YES_OPTION) {
                EmployeeDepartmentHistory record = new EmployeeDepartmentHistory();
                record.setEmployeeID(selectedRecord.getEmployeeID());
                record.setDepartmentID(selectedRecord.getDepartmentID());
                record.setShiftID(selectedRecord.getShiftID());
                record.setStartDate(selectedRecord.getStartDate());
                record.setEndDate(selectedRecord.getEndDate());
                boolean returnVal = driver.deleteEmployeeDepH(record);
                if (returnVal) {
                    schedulerTableModel.deleteViewScheduler(selectedRecord);
                    JOptionPane.showMessageDialog(this, "Deleted");
                } else {
                    JOptionPane.showMessageDialog(this, "Cannot delete !", "Error", JOptionPane.WARNING_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "You migh select one record !", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public boolean containtPK(int eid, int did, int sid, Date date) {
        return schedulerTableModel.containtPK(eid, did, sid, date);
    }

    private void setSelectedEmployee() {
        int index = schedulerTable.getSelectedRow();
        if (index != -1) {
            selectedRecord = schedulerTableModel.getViewSchedulerAt(index);
        } else {
            selectedRecord = null;
        }
        editSchedulerPanel.setSelectedRecord(selectedRecord);
    }

    private void filter() {
        try {
            String id = txtID.getText();
            String name = txtName.getText();
            schedulerTableModel.filter(id, name, 0, 0);
        } catch (Exception e) {
        }
    }

    public void closeTarget() {
        switchTarget(1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private DepartmentHeadDriver driver;
    private ViewESchedulerTableModel schedulerTableModel;
    private CreateESchedulerPanel createSchedulerPanel;
    private EditESchedulerPanel editSchedulerPanel;
    private ViewScheduler selectedRecord;
    private int target;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        cbActions = new javax.swing.JComboBox();
        viewControl = new javax.swing.JScrollPane();
        schedulerTable = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("ID:");

        txtID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtIDCaretUpdate(evt);
            }
        });

        jLabel2.setText("Name:");

        txtName.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtNameCaretUpdate(evt);
            }
        });

        cbActions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Actions", "View", "Add", "Edit", "Delete" }));
        cbActions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbActionsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 232, Short.MAX_VALUE)
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        schedulerTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        schedulerTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                schedulerTableMouseClicked(evt);
            }
        });
        viewControl.setViewportView(schedulerTable);

        add(viewControl, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void cbActionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbActionsActionPerformed
    swithAction();
}//GEN-LAST:event_cbActionsActionPerformed

private void txtIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtIDCaretUpdate
    filter();
}//GEN-LAST:event_txtIDCaretUpdate

private void txtNameCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtNameCaretUpdate
    filter();
}//GEN-LAST:event_txtNameCaretUpdate

private void schedulerTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_schedulerTableMouseClicked
    setSelectedEmployee();
}//GEN-LAST:event_schedulerTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbActions;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTable schedulerTable;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtName;
    private javax.swing.JScrollPane viewControl;
    // End of variables declaration//GEN-END:variables
}
